package com.zhongjinkang.rhythm.AppView.Fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.zhongjinkang.rhythm.AppView.Adapter.HomeAdapter
import com.zhongjinkang.rhythm.databinding.FragmentHomeBinding
import com.zhongjinkang.rhythm.uiBean.PortrayBean
import com.zhongjinkang.rhythm.viewModel.HomeViewModel


class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>(
    FragmentHomeBinding::inflate, HomeViewModel::class.java, false
) {
    private var adapter: HomeAdapter? = null;



    override fun toRefreshUI(viedModel: HomeViewModel?) {
        viedModel?.let { it ->
            it.list.let { it1 ->
                it1.observe(this) {
                    toShowAdapter(it)//这是做UI显示
                }


            }

        }
    }

    override fun initFragment(
        binding: FragmentHomeBinding,
        viedModel: HomeViewModel?,
        savedInstanceState: Bundle?
    ) {


        binding.sr.setOnRefreshListener {
            binding.sr.postDelayed({
                binding.sr.isRefreshing = false
//                viedModel?.getList2()
            }, 2000)
        }




//
//
    }

    private fun toShowAdapter(list: List<PortrayBean>) {
        if (adapter == null) {
            binding.rv.layoutManager =
                StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            adapter = HomeAdapter(list, context!!)

        } else {
            adapter?.list = list;
        }

        binding.rv.layoutManager =
            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        binding.rv.adapter = adapter

    }





}
